package cn.swust.score.controller;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONObject;

import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import cn.swust.score.basepojo.ResultResponse;
import cn.swust.score.pojo.AllScoreExcel;
import cn.swust.score.pojo.CandidateGrade;

import cn.swust.score.pojo.ShowGrade;
import cn.swust.score.pojo.StageGrade;
import cn.swust.score.pojo.StageScoreExcel;
import cn.swust.score.service.GradeAdminService;
import cn.swust.score.service.StageService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

@Api(tags = "管理员评分")
@RestController
@Validated
@RequestMapping("/admin/grade")
public class GradeAdminController extends BaseController {
	public static class allInfo {
		Integer stageId;
		Integer refreshid = 0;
		Integer scoreInfoId = 1;

		public Integer getStageId() {
			return stageId;
		}

		public void setStageId(Integer stageId) {
			this.stageId = stageId;
		}

		public Integer getRefreshid() {
			return refreshid;
		}

		public void setRefreshid(Integer refreshid) {
			this.refreshid = refreshid;
		}

		public Integer getScoreInfoId() {
			return scoreInfoId;
		}

		public void setScoreInfoId(Integer scoreInfoId) {
			this.scoreInfoId = scoreInfoId;
		}
	}

	@Autowired
	public GradeAdminService gradeadminservice;
	@Autowired
	StageService stageService;
	@PreAuthorize("hasAuthority('ADMIN')")
	@ApiOperation(value = "获取阶段打分信息")
	@PostMapping("/get")
	public ResultResponse<List<ShowGrade>> getCandidat(
			@ApiParam(required = true, value = "评分信息") @Valid @RequestBody allInfo info, HttpServletRequest request) {
		if (info.getRefreshid() != 0) {
			if (info.getRefreshid() > this.getRefreshId(request)) {
				this.setRefreshId(request, info.getRefreshid());
				return new ResultResponse<List<ShowGrade>>(gradeadminservice.getCandidate(info.getStageId()));
			} 
			else
				return new ResultResponse<List<ShowGrade>>(gradeadminservice.getCandidate(info.getStageId()));
		} else {
			this.setRefreshId(request, 0);
			return new ResultResponse<List<ShowGrade>>(gradeadminservice.getCandidate(info.getStageId()));
		}
	}
	@PreAuthorize("hasAuthority('ADMIN')")
	@ApiOperation(value = "获取某个阶段打分信息")
	@PostMapping("/one_stage")
	public ResultResponse<List<StageGrade>> getStageGrade(
			@ApiParam(required = true, value = "评分信息") @Valid @RequestBody allInfo info) {
		return new ResultResponse<List<StageGrade>>(gradeadminservice.getStageGrade(info.getStageId()));
	}
	@PreAuthorize("hasAuthority('ADMIN')")
	@ApiOperation(value = "获取某个阶段打分信息")
	@GetMapping("/one_excel")
	public void stageExcel(HttpServletRequest request, HttpServletResponse response) throws IOException {
		int stageId = Integer.parseInt(request.getParameter("stageId"));

		List<StageGrade> list = gradeadminservice.getStageGrade(stageId); 
		if (list != null) { 
			List<StageScoreExcel> excelList = new ArrayList<StageScoreExcel>();
			for (int i = 0; i < list.size(); i++) {
				StageScoreExcel excel = new StageScoreExcel();
				BeanUtils.copyProperties(list.get(i), excel);
				excelList.add(excel);
			}

			Workbook workbook = ExcelExportUtil.exportExcel(new ExportParams(), StageScoreExcel.class, excelList);

			// 告诉浏览器用什么软件可以打开此文件
			response.setHeader("content-Type", "application/vnd.ms-excel");
			// 下载文件的默认名称
			response.setHeader("Content-Disposition", "attachment;filename=stepScore.xls");
			workbook.write(response.getOutputStream());
		} else {
			ResultResponse<String> temp = new ResultResponse<String>(0, "No Data", null);
			response.getWriter().write(JSONObject.toJSONString(temp));
		}
	}
	@PreAuthorize("hasAuthority('ADMIN')")
	@ApiOperation(value = "获取所有阶段打分信息")
	@PostMapping("/allgrade")
	public ResultResponse<List<CandidateGrade>> getAllGrade(
			@ApiParam(required = true, value = "评分信息") @Valid @RequestBody allInfo info) {
		return new ResultResponse<List<CandidateGrade>>(gradeadminservice.getAllGrade(info.getScoreInfoId()));
	}
	@PreAuthorize("hasAuthority('ADMIN')")
	@ApiOperation(value = "获取所有阶段打分信息")
	@GetMapping("/allexcel")
	public void excel(HttpServletRequest request, HttpServletResponse response) throws IOException {
		int scoreInfoId = Integer.parseInt(request.getParameter("scoreInfoId"));
		List<CandidateGrade> list = gradeadminservice.getAllGrade(scoreInfoId);
		if (list != null) {

			List<AllScoreExcel> excelList = new ArrayList<AllScoreExcel>();
			for (int i = 0; i < list.size(); i++) {
				AllScoreExcel excel = new AllScoreExcel();
				BeanUtils.copyProperties(list.get(i), excel);
				excelList.add(excel);
			}
			Workbook workbook = ExcelExportUtil.exportExcel(new ExportParams(), AllScoreExcel.class, excelList);
			// 告诉浏览器用什么软件可以打开此文件
			response.setHeader("content-Type", "application/vnd.ms-excel");
			// 下载文件的默认名称
			response.setHeader("Content-Disposition", "attachment;filename=allScore.xls");
			workbook.write(response.getOutputStream());
		} else {
			ResultResponse<String> temp = new ResultResponse<String>(0, "No Data", null);
			response.getWriter().write(JSONObject.toJSONString(temp));
		}
	}
}
